package com.huawei.dc.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.huaweicloud.sdk.core.exception.ServerResponseException;
import com.huaweicloud.sdk.dc.v3.DcClient;
import com.huaweicloud.sdk.dc.v3.model.CreateVirtualGateway;
import com.huaweicloud.sdk.dc.v3.model.CreateVirtualGatewayRequest;
import com.huaweicloud.sdk.dc.v3.model.CreateVirtualGatewayRequestBody;
import com.huaweicloud.sdk.dc.v3.model.CreateVirtualGatewayResponse;
import com.huaweicloud.sdk.dc.v3.model.DeleteVirtualGatewayRequest;
import com.huaweicloud.sdk.dc.v3.model.DeleteVirtualGatewayResponse;
import com.huaweicloud.sdk.dc.v3.model.ListVirtualGatewaysRequest;
import com.huaweicloud.sdk.dc.v3.model.ListVirtualGatewaysResponse;
import com.huaweicloud.sdk.dc.v3.model.ShowVirtualGatewayRequest;
import com.huaweicloud.sdk.dc.v3.model.ShowVirtualGatewayResponse;
import com.huaweicloud.sdk.dc.v3.model.UpdateVirtualGateway;
import com.huaweicloud.sdk.dc.v3.model.UpdateVirtualGatewayRequest;
import com.huaweicloud.sdk.dc.v3.model.UpdateVirtualGatewayRequestBody;
import com.huaweicloud.sdk.dc.v3.model.UpdateVirtualGatewayResponse;
import com.huaweicloud.sdk.dc.v3.region.DcRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

public class VirtualGatewayDemo {

    private static final Logger logger = LoggerFactory.getLogger(VirtualGatewayDemo.class);
    private static final String AK = "<your AK>";
    private static final String SK = "<your SK>";
    private static final String PROJECT_ID = "<your project id>";
    private static DcClient client = getDcClient();

    /* 获取DC请求客户端 */
    public static DcClient getDcClient() {
        return DcClient.newBuilder()
                .withCredential(new BasicCredentials()
                        .withAk(AK)
                        .withSk(SK)
                        .withProjectId(PROJECT_ID)
                ).withRegion(DcRegion.CN_NORTH_4)
                .build();
    }

    public static void main(String[] args) {

        // 1,创建虚拟网关
        CreateVirtualGatewayResponse response = createVirtualGateway();

        // 2,查询虚拟网关详情
        showVirtualGateway(response.getVirtualGateway().getId());

        // 3,修改虚拟网关
        updateVirtualGateway(response.getVirtualGateway().getId());

        // 4,查询虚拟网关列表
        listVirtualGateways(response.getVirtualGateway().getId());

        // 5,删除虚拟网关
        deleteVirtualGateway(response.getVirtualGateway().getId());
    }

    private static CreateVirtualGatewayResponse createVirtualGateway() {
        CreateVirtualGateway createVirtualGateway = new CreateVirtualGateway()
                .withVpcId("<your vpc id>")
                .withName("<your virtual-gateway name>")
                .withLocalEpGroup(Arrays.asList("<your local-ep-group>"))
                .withDescription("<your virtual-gateway description>");
        CreateVirtualGatewayRequest request = new CreateVirtualGatewayRequest()
                .withBody(new CreateVirtualGatewayRequestBody().withVirtualGateway(createVirtualGateway));
        CreateVirtualGatewayResponse response = null;
        try {
            response = client.createVirtualGateway(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
        return response;
    }

    private static void showVirtualGateway(String virtualGatewayId) {
        ShowVirtualGatewayRequest request = new ShowVirtualGatewayRequest().withVirtualGatewayId(virtualGatewayId);
        try {
            ShowVirtualGatewayResponse response = client.showVirtualGateway(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
    }

    private static void updateVirtualGateway(String virtualGatewayId) {
        UpdateVirtualGatewayRequest request = new UpdateVirtualGatewayRequest()
                .withVirtualGatewayId(virtualGatewayId)
                .withBody(new UpdateVirtualGatewayRequestBody().withVirtualGateway(new UpdateVirtualGateway()
                        .withName("<your new virtual-gateway name>")
                        .withDescription("<your new virtual-gateway description>")));
        try {
            UpdateVirtualGatewayResponse response = client.updateVirtualGateway(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
    }

    private static void listVirtualGateways(String virtualGatewayId) {
        ListVirtualGatewaysRequest request = new ListVirtualGatewaysRequest().withId(Arrays.asList(virtualGatewayId));
        try {
            ListVirtualGatewaysResponse response = client.listVirtualGateways(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
    }

    private static void deleteVirtualGateway(String virtualGatewayId) {
        DeleteVirtualGatewayRequest request = new DeleteVirtualGatewayRequest().withVirtualGatewayId(virtualGatewayId);
        try {
            DeleteVirtualGatewayResponse response = client.deleteVirtualGateway(request);
            logger.info(response.toString());
        } catch (ClientRequestException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        } catch (ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.getMessage());
        }
    }
}